package com.example.greedysnake.views;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.example.greedysnake.datas.SnakeGamingData;
import com.example.greedysnake.datas.SnakeRectPosition;

public class SnakeGamingSurfaceView extends SurfaceView {
    private SurfaceHolder mSurfaceHolder;
    private Paint paint;

    public SnakeGamingSurfaceView(Context context) {
        super(context);
        initView();
    }

    public SnakeGamingSurfaceView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public SnakeGamingSurfaceView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStyle(Paint.Style.FILL);
        paint.setStrokeWidth(2);
        // 返回SurfaceHolder，提供对该SurfaceView的基础表面的访问和控制
        this.mSurfaceHolder = getHolder();
        //画布透明处理
        this.setZOrderOnTop(true);
        this.mSurfaceHolder.setFormat(PixelFormat.TRANSLUCENT);
    }

    // 更新界面
    public void update(SnakeRectPosition[][] snakeRectPositionArray){
        Canvas mCanvas = mSurfaceHolder.lockCanvas();
        if (null != mCanvas) {
            mCanvas.drawColor(Color.BLACK);
            for (SnakeRectPosition[] snakeRectPositions : snakeRectPositionArray) {
                for (int j = 0; j < snakeRectPositionArray[0].length; j++) {
                    if (snakeRectPositions[j].isFill()){
                        paint.setColor(snakeRectPositions[j].getColor());
                    }else {
                        paint.setColor(Color.WHITE);
                    }
                    mCanvas.drawRect(snakeRectPositions[j].getLeft(), snakeRectPositions[j].getTop(),
                            snakeRectPositions[j].getRight(), snakeRectPositions[j].getBottom(), paint);
                }
            }
            mSurfaceHolder.unlockCanvasAndPost(mCanvas);
        }
    }

    public void update(SnakeGamingData snakeGamingData){
        snakeGamingData.init(getWidth());
        SnakeRectPosition[][] snakeRectPositionArray = snakeGamingData.updateCurrentGamingData();
        Canvas mCanvas = mSurfaceHolder.lockCanvas();
        if (null != mCanvas) {
            mCanvas.drawColor(Color.BLACK);
            for (SnakeRectPosition[] snakeRectPositions : snakeRectPositionArray) {
                for (int j = 0; j < snakeRectPositionArray[0].length; j++) {
                    if (snakeRectPositions[j].isFill()){
                        paint.setColor(snakeRectPositions[j].getColor());
                    }else {
                        paint.setColor(Color.WHITE);
                    }
                    mCanvas.drawRect(snakeRectPositions[j].getLeft(), snakeRectPositions[j].getTop(),
                            snakeRectPositions[j].getRight(), snakeRectPositions[j].getBottom(), paint);
                }
            }
            mSurfaceHolder.unlockCanvasAndPost(mCanvas);
        }
    }
}
